import Taro from '@tarojs/taro';
import { View, Textarea, Button } from '@tarojs/components';
import { AtNoticebar, AtForm, AtToast } from 'taro-ui';
import { useState, useEffect } from 'react';
import "taro-ui/dist/style/components/button.scss"; // 按需引入
import "taro-ui/dist/style/components/noticebar.scss";
import "taro-ui/dist/style/components/textarea.scss";
import "taro-ui/dist/style/components/toast.scss";
import "taro-ui/dist/style/components/icon.scss";
import "taro-ui/dist/style/components/message.scss";

import { leaveMsg } from '@/api/application/onlineMsg';

import Header from '@/components/header';
import Return from '@/components/Return';
import css from './index.module.less';

/**
 * 应用/在线留言
 */
export default function OnlineMsg() {
  const [isOpened, setIsOpened] = useState(false);
  const [opened, setOpened] = useState(false);

  const [state, setState] = useState('');
  const [userInfo, setUserInfo] = useState({});
  useEffect(() => {
    Taro.getStorage({
      key: 'userInfo',
      success: function (a) {
        setUserInfo(a.data);
      }
    });
  }, []);


  const submitOlnMsg = (e) => {

    const tempOpen = false;
    if (userInfo.isAudited === 1) {
      const message = {
        partyId: userInfo.partyId,
        leaveWordsContent: e[0].detail.value.leaveWordsContent,
      }
      leaveMsg(message).then((res) => {
        if (res.code === 200) {
          setIsOpened(!tempOpen);
          setState('success');

        } else {
          setState('error');
        }

      })
    } else {
      setOpened(true);
    }
  }

  const changeOpened = () => {
    setOpened(false);
  }
  const changeIsopened = () => {
    setIsOpened(false);
  }

  if (state === 'success') {
    Taro.navigateBack();
  }

  return (
    <View className={css.home}>
      <Header message='在线留言' left='140px' />
      <Return />
      <AtForm className={css.content} onSubmit={submitOlnMsg}>
        <View className={css.noticebar}>
          <AtNoticebar icon='alert-circle' marquee>
            只有实名认证通过的,才能发表留言
          </AtNoticebar>
        </View>
        <View className={css.textareabox}>
          <Textarea
            count={false}
            name='leaveWordsContent'
            type="text"
            placeholder='请输入您想要留言的内容'
            className={css.area}
            maxlength={-1}
          />
        </View>
        <View className={css.userBox}>
          <View className={css.user}>
            姓名：{userInfo.name}
          </View>
        </View>
        <View className={css.buttonBox}>
          <Button formType='submit' className={css.button} >提交</Button>
        </View>
      </AtForm>
      <AtToast isOpened={isOpened} text={state} status={state} icon="alert-circle" onClose={changeIsopened}></AtToast>
      <AtToast isOpened={opened} text='您还没有完成认证' icon="alert-circle" onClose={changeOpened}></AtToast>


    </View>
  );
}